<form class="form-inline" method="get">

    <div class="form-group col-xs-2"  style="width: 20%;">
        <div class="col-xs-12">
            <input class="form-control" name="id" value="{$id}"
                   placeholder="商品ID">
        </div>
    </div>
   
    <div class="form-group col-xs-2"  style="width: 20%;">
        <div class="col-xs-12">
            <input class="form-control" name="title" value="{$title}"
                   placeholder="商品名称">
        </div>
    </div>

    {include file="public/categorySelector" one-name="category_id1" two-name="category_id2" three-name="category_id3" /}

    <br><br><br>

    <div class="form-group col-xs-2">
        <select id="status-selector" name="status" class="form-control" tabindex="-1">
            <option value="">
                无选择
            </option>
            <option value="1" {if condition="$status != '' && $status==1"}selected{/if} >上架</option>
            <option value="0" {if condition="$status != '' && $status==0"}selected{/if} >下架</option>
        </select>
    </div>


    <div class="form-group col-xs-6">
        <button type="submit" class="btn btn-success">
            搜索
        </button>

        <button type="button" class="btn btn-primary" onclick="modal('{:url(\'add\')}','添加商品','60%','90%')">
            添加商品
        </button>

        <!--<button type="button" class="btn btn-primary" onclick="modal('{:url(\'copy\')}','抓取商品','60%','90%')">-->
            <!--抓取商品-->
        <!--</button>-->
    </div>
</form>
<br>
<div class="row">

    <div class="col-xs-12">
        <div class="btn-group" role="group" aria-label="...">
            <button type="button" class="btn btn-default batch-action disabled"
                    onclick="ajaxBatch('{:url(\'allUp\')}')">
                <i class="fa fa-arrow-up"></i>
                批量上架
            </button>
            <button type="button" class="btn btn-default batch-action disabled"
                    onclick="ajaxBatch('{:url(\'allDown\')}')">
                <i class="fa fa-arrow-down"></i>
                批量下架
            </button>
        </div>

    </div>


</div>

<hr>

<table class="table table-bordered table-striped">
    <thead>
    <tr>
        <td>
            <input type="checkbox" data-toggle="check-all">
        </td>
        <td>ID</td>
        <td>商品名称</td>
        <td>所属分类</td>
        <td>缩略图</td>
        <td>默认售价</td>
        <td>库存</td>
        <td>销量</td>
        <td>浏览</td>
        <td>排序</td>
        <td>上下架</td>
        <td>首页推荐</td>
        <td>创建时间<br>更新时间</td>
        <td>操作</td>
    </tr>
    </thead>
    <tbody>
    {volist name="list" id="item"}
    <tr>
        <td>
            <input type="checkbox" data-toggle="check-item" data-good-id="{$item.id}">
        </td>
        <td>{$item.id}</td>
        <td style="width: 200px;">{$item.title}</td>
        <td>
            {$item.c1_title|get_visible_title=$item.c1_visible}
            {if $level>=2}
            - {$item.c2_title|get_visible_title=$item.c2_visible}
            {/if}
            {if $level>=3}
            - {$item.c3_title|get_visible_title=$item.c3_visible}
            {/if}
        </td>
        <td>
            <a href="javascript:void(0);" onclick="ejectImg('{$item.img_url}','商品{$item.title}logo');">
                <img style="width: 100px;" src="{$item.img_url}" />
            </a>
        </td>
        <td>￥{$item.price}</td>
        <td>{$item.stock}</td>
        <td>
            <p>显示：{$item.sales_nums}</p>
            <p>真实：{$item.real_sales}</p>
        </td>
        <td>{$item.view_nums}</td>
        <td>{$item.display_order}</td>
        <td>
            {if $item["status"]}
            <label class="label label-success" data-toggle="ajaxSwitch" data-switch-status="1"
                   data-switch-value-0="1|上架|label label-success|{:url('status',array('id'=>$item['id'],'status'=>1))}"
                   data-switch-value-1="0|下架|label label-danger|{:url('status',array('id'=>$item['id'],'status'=>0))}">上架</label>
            {else}
            <label class="label label-danger" data-toggle="ajaxSwitch" data-switch-status="0"
                   data-switch-value-0="1|上架|label label-success|{:url('status',array('id'=>$item['id'],'status'=>1))}"
                   data-switch-value-1="0|下架|label label-danger|{:url('status',array('id'=>$item['id'],'status'=>0))}">下架</label>
            {/if}
        </td>
        <td>
            {if $item["is_recommend"] eq 1}
            <label class="label label-success" data-toggle="ajaxSwitch" data-switch-status="1"
                   data-switch-value-2="1|推荐|label label-success|{:url('recommend',array('id'=>$item['id'],'recommend'=>1))}"
                   data-switch-value-1="2|不推荐|label label-danger|{:url('recommend',array('id'=>$item['id'],'recommend'=>2))}">推荐</label>
            {else}
            <label class="label label-danger" data-toggle="ajaxSwitch" data-switch-status="2"
                   data-switch-value-2="1|推荐|label label-success|{:url('recommend',array('id'=>$item['id'],'recommend'=>1))}"
                   data-switch-value-1="2|不推荐|label label-danger|{:url('recommend',array('id'=>$item['id'],'recommend'=>2))}">不推荐</label>
            {/if}
        </td>
        <td>
            <p>{$item.create_time|show_datetime}</p>
            <p>{$item.update_time|show_datetime}</p>
        </td>
        <td>
            <div class="btn-group">
                <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    操作
                    <span class="caret"></span>
                </button>
                <ul class="dropdown-menu">
                    <li>
                        <a href="javascript:void(0)" onclick="modal('{:url(\'edit\',array(\'id\'=>$item[\'id\']))}', '编辑商品','60%','90%')">
                            编辑
                        </a>
                    </li>
                    <li>
                        <a href="javascript:void(0)" data-toggle="confirm"
                           data-title="删除商品"
                           data-content="确认删除商品？删除后不可恢复，请谨慎操作！"
                           data-url="{:url('remove',array('id'=>$item['id']))}">
                            删除
                        </a>
                    </li>

                    <li role="separator" class="divider"></li>
                </ul>
            </div>
        </td>
    </tr>
    {/volist}
    </tbody>
</table>

<div class="row">
    <div style="text-align: right;">
        {$list->render()}
    </div>
</div>

<script src="__PUBLIC__/vendors/select2/js/select2.min.js"></script>
<script type="application/javascript">
    $(function () {

        $("#status-selector").select2({
            placeholder: "选择上下架",
            allowClear: true
        });


        $("[data-toggle='check-all']").on("click", function () {
            //判断是否被选中
            if ($(this).prop("checked"))
            {
                $("[data-toggle='check-item']").prop("checked", true);
            }
            else
            {
                $("[data-toggle='check-item']").prop("checked", false);
            }

            //更新批量按钮状态
            updateBatchActions();
        });

        $("[data-toggle='check-item']").on("click", function () {

            //判断是否被选中
            if ($(this).prop("checked"))
            {
                //获取所有的此类按钮数量
                var total = $("[data-toggle='check-item']").length;
                var checked = $(":checked[data-toggle='check-item']").length;

                if (checked == total)
                {
                    $("[data-toggle='check-all']").prop("checked", true);
                }
            }
            else
            {
                $("[data-toggle='check-all']").prop("checked", false);
            }

            //更新批量按钮状态
            updateBatchActions();
        })
    });
    //更新批量操作按钮状态
    function updateBatchActions()
    {
        if ($(":checked[data-toggle='check-item']").length > 0)
        {
            $(".batch-action").removeClass("disabled");
        }
        else
        {
            $(".batch-action").addClass("disabled");
        }
    }

    //批量操作
    function ajaxBatch(url)
    {
        //商品ID数组
        var goodList = [];
        //获取所有选中的商品
        $(":checked[data-toggle='check-item']").each(function(){
            var id = $(this).attr("data-good-id");
            goodList.push(id);
        });
        if(goodList.length<=0){
            showErrorMessage('请先选择商品');
            return;
        }
        //进行ajax请求
        ajaxRequest(url, {id_list: goodList.join(",")}, function () {
            location.reload(true);
        },true);
    }
</script>